package org.petstore.persistence;

import java.sql.*;

public class DBUtil {
    private static final String driveString = "com.mysql.cj.jdbc.Driver";
    private static final String connectionString = "jdbc:mysql://127.0.0.1:3306/mypetstore?useUnicode=true&characterEncoding=utf8&useSSL=false";
    private static final String username = "root";
    private static final String password = "kjj4150019...";

    //获取连接
    public static Connection getConnection() throws Exception {
        Connection connection = null;

        try {
            Class.forName(driveString);
            connection = DriverManager.getConnection(connectionString, username, password);
        } catch (Exception e) {
            throw e;
        }

        return connection;
    }

    public static void closeStatement(Statement statement) throws Exception {
        statement.close();
    }

    public static void closePreparedStatement(PreparedStatement pStatement) throws Exception {
        pStatement.close();
    }

    public static void closeResultSet(ResultSet resultSet) throws Exception {
        resultSet.close();
    }

    //关闭连接
    public static void closeConnection(Connection connection) throws Exception {
        connection.close();
    }

//    //测试
//    public static void main(String[] args)throws Exception{
//        Connection conn=DBUtil.getConnection();
//        System.out.print(conn);
//    }
}